<?php
/**
 * @version 0.8.3 2010-10-07
 * @package Joomla
 * @subpackage LeagueResults
 * @copyright (C)  2008, 2009, 2010 Henryk Drozd
 * @license GNU/GPL, see LICENSE.php
 * LeagueResults is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.

 * LeagueResults is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

class lrControllerTableheaders extends lrController {

	function __construct()
	{
		
		parent::__construct();
		
			$this->registerTask( 'add', 'edit' );
			$this->registerTask( 'apply', 'save' );
	}
		
	function publish()
	{
		$cid 	= JRequest::getVar( 'cid', array(0), 'post', 'array' );

		if (!is_array( $cid ) || count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}

		$model = $this->getModel('tableheaders');
		print_r ($model);
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError()."'); window.history.go(-1); </script>\n";
		}

		$total = count( $cid );
		$msg 	= $total.' '.JText::_('TABLE PUBLISHED');

		$this->setRedirect( 'index.php?option=com_lr&view=tableheaders', $msg );
	}
	function unpublish()
	{
		$cid 	= JRequest::getVar( 'cid', array(0), 'post', 'array' );

		if (!is_array( $cid ) || count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to unpublish' ) );
		}

		$model = $this->getModel('tableheaders');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError()."'); window.history.go(-1); </script>\n";
		}

		$total = count( $cid );
		$msg 	= $total.' '.JText::_('TABLE UNPUBLISHED');

		$this->setRedirect( 'index.php?option=com_lr&view=tableheaders', $msg );
	}

	function cancel()
	{
		// Check for request forgeries
		JRequest::checkToken() or die( 'Invalid Token' );
		


		$this->setRedirect( 'index.php?option=com_lr&view=tableheaders' );
	}

	function remove()
	{
		global $option;

		$cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );

		if (!is_array( $cid ) || count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('tableheaders');
		$model->delete($cid);
		$msg = count($cid).' '.JText::_('TABLE DELETED');

		$cache = &JFactory::getCache('com_lr');
		$cache->clean();

		$this->setRedirect( 'index.php?option=com_lr&view=tableheaders', $msg );
	}

	function edit( )
	{
	
		JRequest::setVar( 'view', 'tableheader' );
		JRequest::setVar( 'hidemainmenu', 1 );

		$model 	= $this->getModel('tableheader');
		$user	=& JFactory::getUser();

		
		parent::display();
	}
	function save()
	{
		// Check for request forgeries
		JRequest::checkToken() or die( 'Invalid Token' );

		$task		= JRequest::getVar('task');

		$post = JRequest::get( 'post' );

		$model = $this->getModel('tableheader');
//print_r($post);sssss();
		if ($returnid = $model->store($post)) {

			switch ($task)
			{
				case 'apply' :
					$link = 'index.php?option=com_lr&controller=tableheaders&view=tableheader&hidemainmenu=1&cid[]='.$returnid;
					break;

				default :
					$link = 'index.php?option=com_lr&view=tableheaders';
					break;
			}
			$msg	= JText::_( 'TABLE SAVED');

			$cache = &JFactory::getCache('com_lr');
			$cache->clean();

		} else {

			$msg 	= '';
			$link = 'index.php?option=com_lr&view=tableheaders';

		}



		$this->setRedirect( $link, $msg );
 	}
	
}
?>
